package sample.client.view;

import gwtupload.client.Uploader;

import sample.client.CMModel;
import sample.client.ClientList;
import sample.client.event.SelectClientEvent;
import sample.client.event.SetEnabledEvent;
import sample.shared.Consts;
import sample.shared.result.Client;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.ComboBox;

public class SearchPanel extends FramedPanel
{
  private ComboBox<Client> searchClientCombo = ClientList.createClientCombo();
  private final Label codeLabel = new Label("");
  private final boolean showBorders = false;
  private final TextButton gotoButton = new TextButton("Go");
  private Uploader gwtupload = new Uploader();
  private TextButton uploadButton = new TextButton(Consts.LABEL_UPLOAD);
  private FormPanel fakeForm;

  public SearchPanel(CMModel model) {
    setHeadingText("Search");
    VerticalLayoutContainer l = new VerticalLayoutContainer();
    VerticalLayoutData vlay =
        new VerticalLayoutData(-1, -1, new Margins(20, 20, 20, 20));

    l.add(createFormPane(), vlay);
    l.setBorders(showBorders);
    l.setScrollMode(ScrollMode.AUTO);

    fakeForm = new FormPanel();
    fakeForm.setVisible(false);
    l.add(fakeForm);

    add(l);
    addButton(gotoButton);
    addButton(uploadButton);
    bind(model);

    gotoButton.setEnabled(false);
  }

  IsWidget createFormPane() {
    codeLabel.setWidth("350");

    VerticalLayoutContainer l = new VerticalLayoutContainer();

    l.setBorders(showBorders);
    VerticalLayoutData vlay =
        new VerticalLayoutData(-1, -1, new Margins(10, 0, 10, 0));
    l.add(Utils.createFormItem(Consts.LABEL_CLIENT_NAME, searchClientCombo),
        vlay);
    l.add(Utils.createFormItem(Consts.LABEL_CLIENT_CODE, codeLabel), vlay);
    l.add(Utils.createFormItem(Consts.LABEL_UPLOAD, gwtupload), vlay);

    SimpleContainer c = new SimpleContainer();
    c.add(l);
    return c;
  }

  private void bind(final CMModel model) {

    gotoButton.addSelectHandler(new SelectHandler() {
      @Override
      public void onSelect(SelectEvent event) {
        LayoutPanel l = new LayoutPanel();
        l.add(new Hidden("clientCode", model.getClientCodeText()));
        fakeForm.setWidget(l);
        fakeForm.setAction("goto");
        fakeForm.setMethod(FormPanel.METHOD_GET);
        fakeForm.submit();
        fakeForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
          @Override
          public void onSubmitComplete(SubmitCompleteEvent event) {
            ClientList.get().update();
          }
        });
      }
    });
    uploadButton.addSelectHandler(new SelectHandler() {
      @Override
      public void onSelect(SelectEvent event) {
        String f = gwtupload.getFileName();
        String f2 = f.substring(f.lastIndexOf("\\") + 1);
        String code = codeLabel.getText();

        gwtupload.add(new Hidden("filename", f2), 0);
        gwtupload.add(new Hidden("clientCode", code), 0);
        gwtupload.submit();
      }
    });
    searchClientCombo.addSelectionHandler(new SelectionHandler<Client>() {
      @Override
      public void onSelection(SelectionEvent<Client> event) {
        Client client = searchClientCombo.getCurrentValue();
        model.setClient(client);
        searchClientCombo.setValue(client);
        model.onClientNameChanged();
      }
    });

    searchClientCombo.addValueChangeHandler(new ValueChangeHandler<Client>() {
      @Override
      public void onValueChange(ValueChangeEvent<Client> event) {
        Client client = searchClientCombo.getCurrentValue();
        model.setClient(client);
        searchClientCombo.setValue(client);
        model.onClientNameChanged();
      }
    });
    HandlerManager eventBus = model.getEventBus();
    eventBus.addHandler(SelectClientEvent.TYPE,
        new SelectClientEvent.SelectClientHandler() {
          @Override
          public void onSelectClient(SelectClientEvent e) {
            codeLabel.setText(model.getClientCodeText());
          }
        });
    eventBus.addHandler(SetEnabledEvent.TYPE,
        new SetEnabledEvent.SetEnabledHandler() {
          @Override
          public void onSetEnabled(SetEnabledEvent e) {
            switch (e.getOpt()) {
              case GOTO:
                gotoButton.setEnabled(e.isEnabled());
                break;
            }
          }
        });
  }
}
